package part3.demo1;

import java.util.Arrays;

/**
 * Created by lie on 2018/3/26.
 * 随手敲敲，练习下手感
 */
public abstract class BubblePractis {

    protected int length;

    public void doSort(){
        if (length <= 1) {
            return;
        }

        for (int lastIndex = length-2; lastIndex >=0 ; lastIndex--) {
            for (int begin = 0; begin<=lastIndex;begin++){
                if (outOfOrder(begin))
                    swap(begin);
            }
        }
    }

    protected abstract void swap(int begin);

    protected abstract boolean outOfOrder(int begin);
}

class IntBDemo extends BubblePractis{
    private int[] array;

    public void setArrayAndSort(int[] array){
        this.length = array.length;
        this.array = array;
        doSort();
    }

    protected void swap(int begin) {
        int temp = array[begin + 1];
        array[begin+1] = array[begin];
        array[begin]  =temp;
    }

    protected boolean outOfOrder(int begin) {
        return array[begin] > array[begin+1];
    }
}

class BTest{
    public static void main(String[] args){
        int[] arrays = {4, 3, 4, 2, 0, 0, 2, 4, 9, 1, -1};
        IntBDemo intBDemo = new IntBDemo();
        intBDemo.setArrayAndSort(arrays);
        System.out.println(Arrays.toString(arrays));
    }

}
